<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mindex extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
	public function getCategoriesFoodDb(){
		$id_restaurent = $this->getParamInt('id_restaurent');
		$id_admin = $this->getParamInt('id_admin');
		$sql = 'SELECT * FROM categories_food WHERE 1=1 ';
		if( !in_array( $id_restaurent,array('',null) ) ){
			$sql .= 'AND id_restaurent = '.$id_restaurent.' ';
		}
		if( !in_array( $id_admin,array('',null) ) ){
			$sql .= 'AND id_admin = '.$id_admin.' ';
		}
		$sql .= 'ORDER BY orderby ASC'; 
		return $this->db->query($sql)->result_object();
	}
	
	public function getListFoodDb(){
		$id_categories = $this->getParamInt('id_categories');
		$id_restaurent = $this->getParamInt('id_restaurent');
		$id_admin = $this->getParamInt('id_admin');
		if( !in_array( $id_categories,array('',null) ) ){
			$sql = 'SELECT c.categories_name, f.* FROM categories_food AS c LEFT JOIN food AS f ON c.id = f.categories_food WHERE f.categories_food = '.$id_categories.' ';
			if( !in_array( $id_restaurent,array('',null) ) ){
				$sql .= 'AND f.id_restaurent = '.$id_restaurent.' ';
			}
			if( !in_array( $id_admin,array('',null) ) ){
				$sql .= 'AND f.id_admin = '.$id_admin.' ';
			}
			$sql .= 'ORDER BY f.orderby ASC'; 
		}else{
			$sql = 'SELECT * FROM food WHERE 1 = 1 ';
			if( !in_array( $id_restaurent,array('',null) ) ){
				$sql .= 'AND id_restaurent = '.$id_restaurent.' ';
			}
			if( !in_array( $id_admin,array('',null) ) ){
				$sql .= 'AND id_admin = '.$id_admin.' ';
			}
			$sql .= 'ORDER BY orderby ASC'; 
		}
		return $this->db->query($sql)->result_object();
	}
	
	public function getFoodDb(){
		$id_admin = $this->getParamInt('id_admin');
		if( !in_array($id_admin,array('',null)) ){
			$data = $this->db->query('SELECT id AS category_id, categories_name AS caregory_name FROM categories_food WHERE id_admin = '.$id_admin.' ORDER BY orderby ASC')->result_object();
			foreach( $data AS $el=>$le ){
				$data_ = $this->db->query('SELECT * FROM food WHERE categories_food = '.$le->category_id)->result_object();
				foreach( $data_ AS $el1=>$le1 ){
					$prices =number_format($le1->price,0,',','.').'đ' ;
					$data_[$el1]->prices = $prices;
					$data_[$el1]->summarys = $this->printstr($le1->summary,12);
				}
				$data[$el]->foods = $data_;
			}
			return $data;
		}else{
			return array();
		}
	}
	
	public function getStaffDb(){
		$id_admin = $this->getParamInt('id_admin');
		if( !in_array($id_admin,array('',null)) ){
			$data = $this->db->query('SELECT * FROM staff WHERE id_admin = '.$id_admin.'')->result_object();
			foreach( $data AS $el=>$le ){
				$data_ = $this->db->query('SELECT * FROM manage_table_staff AS mts INNER JOIN manage_table_group AS mtg ON mts.id_group = mtg.id WHERE mts.id_admin = '.$id_admin.' AND mtg.id_admin = '.$id_admin.' AND mts.id_staff = '.$le->id)->result_object(); 
				$data[$el]->group = $data_;
				foreach ( $data_ AS $el1=>$le1 ){
					$data__ = $this->db->query('SELECT * FROM manage_table AS mt INNER JOIN tbl_table AS tt ON tt.id = mt.id_table WHERE mt.id_admin = '.$id_admin.' AND tt.id_admin = '.$id_admin.' AND mt.id_group = '.$le1->id)->result_object();
					$data_[$el1]->table = $data__;
				}
			}
			return $data;
		}else{
			return array();
		}
	}
	
	public function saveOrderDb(){
		$id_food = $this->getParamInt('id_food');
		$id_restaurant = $this->getParamInt('id_restaurant');
		$id_admin = $this->getParamInt('id_admin');
		$id_table = $this->getParamInt('id_table');
		$id_staff = $this->getParamInt('id_staff');
		$qty = $this->getParamInt('qty');
		if( !in_array($id_food,array('',null,0)) && !in_array($id_restaurant,array('',null,0)) && !in_array($id_admin,array('',null,0)) && !in_array($id_table,array('',null,0)) && !in_array($id_staff,array('',null,0)) && !in_array($qty,array('',null,0)) ){
		 if ( $this->db->query('SELECT fullname FROM user WHERE id = '.$id_admin)->num_rows() == 0 ){
            return array('status' => '202', 'content' => 'Tài khoản không tồn tại');
        }
         if ( $this->db->query('SELECT id FROM staff WHERE id = '.$id_staff)->num_rows() == 0 ){
            return array('status' => '204', 'content' => 'Nhân viên không tồn tại');
		 } 
         if ( $this->db->query('SELECT id FROM tbl_table WHERE id = '.$id_table)->num_rows() == 0 ){
            return array('status' => '205', 'content' => 'Tên bàn không tồn tại');
		 }  
		$dfood = $this->db->query('SELECT * FROM food WHERE id = '.$id_food);
        if( $dfood->num_rows() == 0 ){
            return array('status' => '203', 'content' => 'Món ăn không tồn tại');
        }
			$data = array(
				'name_food' => $dfood->row_object()->name_food,
				'id_food' => $id_food,
				'price' => $dfood->row_object()->price,
				'id_restaurant' => $id_restaurant,
				'id_admin' => $id_admin,
				'id_table' => $id_table,
				'id_staff' => $id_staff,
				'active' => 0
			);
            $count = $this->db->query('SELECT id, qty FROM tracking_menu_food WHERE id_food = '.$id_food.' AND id_restaurant ='.$id_restaurant.' AND id_admin = '.$id_admin.' AND id_table ='.$id_table.' AND id_staff = '.$id_staff.' ');
            if( $count->num_rows() == 0 ){
                $data['qty'] = (int)$qty;
                $data['created'] = date('Y-m-d H:i:s');
                $data['code_cart'] = $this->generateCode(6);
                $data['status'] = (int)0;
                $this->db->insert('tracking_menu_food',$data);
                return array('status' => '200', 'content' => 'Thêm thành công');
			}else{
			     $data['qty'] = (int)$count->row_object()->qty + (int)$qty;
                 $id_ = $count->row_object()->id;
                 $this->db->update('tracking_menu_food',$data,'id ='.$id_);
                 return array('status' => '210', 'content' => 'Cập nhật thành công');
			}
		}else{
			return array('status' => '201', 'content' => 'Bạn điền thiếu thông tin');
		}
	}
	
	public function getSettingAppDb(){
		$id_restaurent = $this->getParamInt('id_restaurent');
		$id_admin = $this->getParamInt('id_admin');
		if( in_array( $id_restaurent,array('',null) ) || in_array( $id_admin,array('',null) )  ){
			return (object)array();
		}else{
			return $this->db->query('SELECT * FROM setting_restaurent WHERE id_restaurent = '.$id_restaurent.' AND id_admin = '.$id_admin.' ')->row_object();
		}
	}
	
	public function saveSettingAppDb(){
		$id_restaurent = $this->getParamInt('id_restaurent');
		$id_admin = $this->getParamInt('id_admin');
		$amount_staff = $this->getParamInt('amount_staff');
		$amount_table = $this->getParamInt('amount_table');
		$type_restaurent = $this->getParamInt('type_restaurent');
		$amount_floors = $this->getParamInt('amount_floors');
		$check = $this->db->query(' SELECT id FROM setting_restaurent WHERE id_restaurent = '.$id_restaurent.' AND id_admin = '.$id_admin.' ')->num_rows();
		if ( $check == 0 ){
			return (object)array('status' => 201, 'description' => 'Không tồn tại trong hệ thống thông tin admin và restaurant'); 
		}else{
			$data = array ( 
				'amount_staff' => (int)$amount_staff,
				'amount_table' => (int)$amount_table,
				'type_restaurent' => (int)$type_restaurent,
				'amount_floors' => (int)$amount_floors,
				'id_admin' => (int)$id_admin,
				'created' => date('Y-m-d H:i:s')
			);
			$run = $this->db->update( 'setting_restaurent', $data, 'id_restaurent = '.$id_restaurent.' ');
			return (object)array('status' => 200, 'description' => 'Cập nhật thành công');	
		}	
	}
	
	public function getOrderDb(){
        $id_restaurant = $this->getParamInt('id_restaurant');
        $id_admin = $this->getParamInt('id_admin');
        if( in_array($id_restaurant,array('',null,0)) || in_array($id_admin,array('',null,0)) ){
            return array('status' => '201', 'description' => 'Điền thiếu thông tin', 'content' => '');
        }else{
            $data = $this->db->query('SELECT tmf.*, tt.name_table, s.name_staff FROM tracking_menu_food AS tmf INNER JOIN tbl_table AS tt ON tmf.id_table = tt.id INNER JOIN staff AS s ON s.id = tmf.id_staff WHERE tmf.id_admin = '.$id_admin.' AND tmf.id_restaurant ='.$id_restaurant)->result_object();
            foreach ( $data AS $el=>$le ){
                $prices =number_format($le->price,0,',','.').'đ' ;
                $data[$el]->prices = $prices;
            }
            return array('status' => '200', 'description' => 'Thành công', 'content' => $data); 
        }
	}
	
    public function updateStatusOrderDb(){
        $id_restaurant = $this->getParamInt('id_restaurant');
        $id_admin = $this->getParamInt('id_admin');
        $order_id = $this->getParamInt('order_id');
        $status = $this->getParamInt('status');
        if( in_array($id_restaurant,array('',null,0)) || in_array($id_admin,array('',null,0)) || in_array($order_id,array('',null,0)) || in_array($status,array('',null))){
            return array('status' => '201', 'description' => 'Điền thiếu thông tin');
        }else{
            $count = $this->db->query('SELECT id FROM tracking_menu_food WHERE id_admin = '.$id_admin.' AND id_restaurant = '.$id_restaurant.' AND id = '.$order_id)->num_rows();
            if( $count == 0 ){
                return array('status' => '202', 'description' => 'Không tồn tại bill');
            }else{
                $data = array( 'status' => (int)$status );
                $this->db->update('tracking_menu_food',$data,'id ='.$order_id);
                return array('status' => '200', 'description' => 'Cập nhật thành công');
            }
        }
    }
	
	public function updateStatusTableDb(){
        $id_restaurant = $this->getParamInt('id_restaurant');
        $id_admin = $this->getParamInt('id_admin');
        $table_id = $this->getParamInt('table_id');
        $status = $this->getParamInt('status');
        if( in_array($id_restaurant,array('',null,0)) || in_array($id_admin,array('',null,0)) || in_array($table_id,array('',null,0)) || in_array($status,array('',null))){
            return array('status' => '201', 'description' => 'Điền thiếu thông tin');
        }else{
            $count = $this->db->query('SELECT id FROM tbl_table WHERE id_admin = '.$id_admin.' AND id_restaurent = '.$id_restaurant.' AND id = '.$table_id)->num_rows();
            if( $count == 0 ){
                return array('status' => '202', 'description' => 'Không tồn tại bàn');
            }else{
                $data = array( 'status' => (int)$status );
                $this->db->update('tbl_table',$data,'id ='.$table_id);
                return array('status' => '200', 'description' => 'Cập nhật thành công');
            }
        }
    }
	
	public function changeTableDb(){
        $id_restaurant = $this->getParamInt('id_restaurant');
        $id_admin = $this->getParamInt('id_admin');
        $old_id = $this->getParamInt('old_id');
        $new_id = $this->getParamInt('new_id');
        $id_staff = $this->getParamInt('id_staff');
        if( in_array($id_restaurant,array('',null,0)) || in_array($id_admin,array('',null,0)) || in_array($old_id,array('',null,0)) || in_array($new_id,array('',null)) || in_array($id_staff,array('',null)) ){
            return array('status' => '201', 'description' => 'Điền thiếu thông tin');
        }else{
            $count = $this->db->query('SELECT * FROM tbl_table WHERE id_restaurent = '.$id_restaurant.' AND ( id = '.$old_id.' OR id ='.$new_id.') ')->num_rows(); //die('333');
            if( $count == 0 ){
                return array('status' => '202', 'description' => 'Bàn không tồn tại');
            }else{
                $this->db->update('tbl_table',array('status' => (int)0 ),'id ='.$old_id);
                $this->db->update('tbl_table',array('status' => (int)1 ),'id ='.$new_id);
                $codecart = $this->db->query('SELECT code_cart FROM tracking_menu_food WHERE id_table = '.$old_id.' AND id_staff = '.$id_staff.' AND status = 0 LIMIT 1')->row_object()->code_cart;
                if( !in_array($codecart,array('',null)) ){
					$darr = array('id_table' => $new_id);
					$this->db->update('tracking_menu_food',$darr, array( 'code_cart' => $codecart, 'id_staff' => $id_staff ) );
					return array('status' => '200', 'description' => 'Chuyển bàn thành công,chuyển bill thành công');
				}else{
					return array('status' => '203', 'description' => 'Chuyển bàn thành công, bàn chưa có bill');
				} ;
            }
        }
	}
    
	public function paymentsTableDb(){
        $id_restaurant = $this->getParamInt('id_restaurant');
        $id_admin = $this->getParamInt('id_admin');
        $id_table = $this->getParamInt('id_table');
        $id_staff = $this->getParamInt('id_staff');
		$status = $this->getParamInt('status'); // 1- thanh toán;2- đã hoàn thành;3- hủy bàn
        if( in_array($id_restaurant,array('',null,0)) || in_array($id_admin,array('',null,0)) || in_array($id_table,array('',null,0)) || in_array($id_staff,array('',null,0)) || in_array($status,array('',null)) ){
            return array('status' => '201', 'description' => 'Điền thiếu thông tin');
        }else{
            $count = $this->db->query('SELECT * FROM tbl_table WHERE id_restaurent = '.$id_restaurant.' AND id = '.$id_table.' AND id_admin = '.$id_admin)->num_rows(); 
			if( $count == 0 ){
                return array('status' => '202', 'description' => 'Bàn không tồn tại');
            }else{
                $count_bill = $this->db->query('SELECT id, code_cart FROM tracking_menu_food WHERE id_table = '.$id_table.' AND id_staff = '.$id_staff.' AND id_restaurant = '.$id_restaurant.' AND id_admin = '.$id_admin.' AND status = 0');
               if( $count_bill->num_rows() == 0  ){
				   return array('status' => '203', 'description' => 'Bàn chưa có bill/đang thanh toán / hoàn thành/hủy bỏ');
			   }else{
				   $code_cart = $count_bill->row_object()->code_cart;
				   $this->db->update('tracking_menu_food',array( 'status' => $status ),array('id_staff' => $id_staff, 'id_table' => $id_table, 'id_admin' => $id_admin, 'id_restaurant' => $id_restaurant, 'code_cart' => $code_cart ));
				   if( $status == 2 ){
					   $this->db->update('tbl_table',array('status' => (int)0),'id ='.$id_table);
				   }
				   return array('status' => '200', 'description' => 'Chuyển trạng thái thành công');
			   }
            }
        }
	}
	
	public function getTableDb(){
        $id_admin = $this->getParamInt('id_admin');
		if( in_array( $id_admin,array('',null) ) ){
			return array('status' => '201', 'description' => 'Điền thiếu thông tin'); 
		}
		$data = $this->db->query('SELECT * FROM categories_floor WHERE 1=1 ')->result_object();
		foreach ( $data AS $el=>$le ){
			$table = $this->db->query('SELECT * FROM tbl_table WHERE id_admin = '.$id_admin.' AND  id_floor = '.$le->id)->result_object();
			$data[$el]->table = $table;
		}
		return array('status' => '200', 'description' => 'Thành công', 'content' => $data); 
	}
	
	public function customerOrderDb(){
		$id_food = $this->getParamInt('id_food');
		$id_admin = $this->getParamInt('id_admin');
		$id_table = $this->getParamInt('id_table');
		$qty = $this->getParamInt('qty');
		if( !in_array($id_food,array('',null,0)) && !in_array($id_admin,array('',null,0)) && !in_array($id_table,array('',null,0)) && !in_array($qty,array('',null,0)) ){
		if ( $this->db->query('SELECT fullname FROM user WHERE id = '.$id_admin)->num_rows() == 0 ){
            return array('status' => '202', 'content' => 'Tài khoản không tồn tại');
        }
        if ( $this->db->query('SELECT id FROM tbl_table WHERE id = '.$id_table)->num_rows() == 0 ){
            return array('status' => '205', 'content' => 'Tên bàn không tồn tại');
		}  
		$dfood = $this->db->query('SELECT * FROM food WHERE id = '.$id_food);
        if( $dfood->num_rows() == 0 ){
            return array('status' => '203', 'content' => 'Món ăn không tồn tại');
        }
		$id_staff = $this->db->query('SELECT mts.id_staff FROM manage_table AS mt INNER JOIN manage_table_staff AS mts ON mts.id_group = mt.id_group WHERE mt.id_table = '.$id_table.' AND mts.id_admin = '.$id_admin.' AND mt.id_admin = '.$id_admin)->row_object()->id_staff;
		if( in_array($id_staff,array('',null,0)) ){
			return array('status' => '204', 'content' => 'Nhân viên không tồn tại');
		}
			$data = array(
				'name_food' => $dfood->row_object()->name_food,
				'id_food' => $id_food,
				'price' => $dfood->row_object()->price,
				'id_admin' => $id_admin,
				'id_table' => $id_table,
				'id_staff' => $id_staff,
				'active' => 0
			);
            $count = $this->db->query('SELECT id, qty FROM tracking_menu_food WHERE id_food = '.$id_food.' AND id_admin = '.$id_admin.' AND id_table ='.$id_table.' AND id_staff = '.$id_staff.' ');
            if( $count->num_rows() == 0 ){
                $data['qty'] = (int)$qty;
                $data['created'] = date('Y-m-d H:i:s');
                $data['code_cart'] = $this->generateCode(6);
                $data['status'] = (int)0;
                $this->db->insert('tracking_menu_food',$data);
                return array('status' => '200', 'content' => 'Thêm thành công');
			}else{
			     $data['qty'] = (int)$count->row_object()->qty + (int)$qty;
                 $id_ = $count->row_object()->id;
                 $this->db->update('tracking_menu_food',$data,'id ='.$id_);
                 return array('status' => '210', 'content' => 'Cập nhật thành công');
			}
		}else{
			return array('status' => '201', 'content' => 'Bạn điền thiếu thông tin');
		}
	}
}